package com.google.gwt.gfx.client.graphics.svg;

import java.util.List;

import com.google.gwt.gfx.client.graphics.Path;
import com.google.gwt.gfx.client.shared.ControlPoint;
import com.google.gwt.gfx.client.shared.DoubleControlPoint;
import com.google.gwt.gfx.client.shared.Point;

public class SVGPath extends Path {

	public SVGPath() {
		super(SVG.createElement("path"));
	}

	private void appendPath(String tail) {
		SVG.setElementProperty(getElement(), "d",
				SVG.getElementProperty(getElement(), "d") + tail);
	}

	public void arcTo(int radiusWidth, int radiusHeight, double rotation,
			boolean isLargeArc, boolean isSweeped, int left, int top) {
		appendPath(" A" + radiusWidth + "," + radiusHeight + " " + rotation
				+ " " + (isLargeArc ? 1 : 0) + "," + (isSweeped ? 1 : 0) + " "
				+ left + "," + top);
	}

	@Override
	public void close() {
		appendPath(" Z");
	}

	@Override
	public void curveTo(List<DoubleControlPoint> cpoints) {
		StringBuilder builder = new StringBuilder();
		for (DoubleControlPoint point : cpoints) {
			builder.append(point.getControlLeft1() + ","
					+ point.getControlTop1() + " " + point.getControlLeft2()
					+ "," + point.getControlTop2() + " " + point.getLeft()
					+ "," + point.getTop() + " ");
		}
		appendPath(" C" + builder.toString());
	}

	@Override
	public void lineTo(List<Point> points) {
		StringBuilder builder = new StringBuilder();
		for (Point point : points) {
			builder.append(point.getLeft() + "," + point.getTop() + " ");
		}
		appendPath(" L" + builder.toString());
	}

	@Override
	public void moveTo(int left, int top) {
		appendPath(" M" + left + "," + top);
	}

	public void quadCurveTo(List<ControlPoint> cpoints) {
		StringBuilder builder = new StringBuilder();
		for (ControlPoint cpoint : cpoints) {
			builder.append(cpoint.getControlLeft() + ","
					+ cpoint.getControlTop() + " " + cpoint.getLeft() + ","
					+ cpoint.getTop() + " ");
		}
		appendPath(" Q" + builder.toString());
	}

	public void smoothCurveTo(List<ControlPoint> cpoints) {
		StringBuilder builder = new StringBuilder();
		for (ControlPoint cpoint : cpoints) {
			builder.append(cpoint.getControlLeft() + ","
					+ cpoint.getControlTop() + " " + cpoint.getLeft() + ","
					+ cpoint.getTop() + " ");
		}
		appendPath(" S" + builder.toString());
	}

	@Override
	public void smoothQuadCurveTo(List<Point> points) {
		StringBuilder builder = new StringBuilder();
		for (Point point : points) {
			builder.append(point.getLeft() + "," + point.getTop() + " ");
		}
		appendPath(" T" + builder.toString());
	}

}
